/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.NhomMA_Model;
import Models.LoaiMA_Model;
import Beans.NhanVien;

import Models.NhatKyHT_Model;

/**
 *
 * @author Venus
 */
public class NhomMA_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMA(langid);
                LoaiMA_Model lmam = new LoaiMA_Model();
                Vector vDSLMA = new Vector();
                vDSLMA = lmam.LayDSLMA(langid);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("DSLMA", vDSLMA);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newfoodgroups.jsp");
                rd.forward(request, response);
            } else if (action.equals("Add")) {
                String maloaima = request.getParameter("maloaima");
                String nhomma = request.getParameter("nhomma");
                int MaNMA = NhomMA_Model.TaoMa();
                NhomMA_Model.AddNhomMA(MaNMA, maloaima, nhomma);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "foodgroups");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.LayFoodGroup(MaNMA);
                request.setAttribute("LangFG", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/foodgrouplang.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowNhomMA")) {
                String item = request.getParameter("item");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.LayDSNMA(langid);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listnhomma.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowLangFG")) {
                String MaNMA = request.getParameter("MaNMA");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.LayFoodGroup(Integer.parseInt(MaNMA));
                request.setAttribute("LangFG", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/foodgrouplang.jsp");
                rd.forward(request, response);
            } else if (action.equals("Edit")) {
                String tennma = request.getParameter("tennma");
                String manma = request.getParameter("manma");
                String mann = request.getParameter("mann");
                NhomMA_Model.Edit(tennma, manma, mann);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "foodgroupsname");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.LayFoodGroup(Integer.parseInt(manma));
                request.setAttribute("LangFG", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/foodgrouplang.jsp");
                rd.forward(request, response);
            } else if (action.equals("UpdateStatusNMA")) {
                String manma = request.getParameter("manma");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                NhomMA_Model.UpdateStatusNMA(manma, status);
                Models.MonAn_Model.UpdateStatusMAFolowNMA(manma, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statusfoodgroup");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.LayDSNMA(langid);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listnhomma.jsp");
                rd.forward(request, response);
            } 
            else if (action.equals("UpdateLMA")) {
                String manma = request.getParameter("manma");
                String malma = request.getParameter("malma");
                String item = request.getParameter("item");
                NhomMA_Model.UpdateLMA(manma, malma);                
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "typefood");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.LayDSNMA(langid);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listnhomma.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Search")) {
                String loaithucan = request.getParameter("loaithucan");
                String tennhommonan = request.getParameter("tennhommonan");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.Search(loaithucan, tennhommonan, status, langid);
                request.setAttribute("status", status);
                request.setAttribute("loaithucan", loaithucan);
                request.setAttribute("tennhommonan", tennhommonan);
                request.setAttribute("item", item);
                request.setAttribute("DSNMA", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenLMA")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.SortTenLMA(langid, order);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenNMA")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.SortTenNMA(langid, order);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTTNMA")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector v = new Vector();
                v = nmam.SortTTNMA(langid, order);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenLMA")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tennhommonan = request.getParameter("tennhommonan");
                String status = request.getParameter("status");
                String loaithucan = request.getParameter("loaithucan");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhomMA_Model kvm = new NhomMA_Model();
                Vector v = new Vector();
                v = kvm.SortSearchTenLMA(langid, order, loaithucan, tennhommonan, status);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("tennhommonan", tennhommonan);
                request.setAttribute("loaithucan", loaithucan);
                request.setAttribute("status", status);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenNMA")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tennhommonan = request.getParameter("tennhommonan");
                String status = request.getParameter("status");
                String loaithucan = request.getParameter("loaithucan");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhomMA_Model kvm = new NhomMA_Model();
                Vector v = new Vector();
                v = kvm.SortSearchTenNMA(langid, order, loaithucan, tennhommonan, status);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("tennhommonan", tennhommonan);
                request.setAttribute("loaithucan", loaithucan);
                request.setAttribute("status", status);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTTNMA")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tennhommonan = request.getParameter("tennhommonan");
                String status = request.getParameter("status");
                String loaithucan = request.getParameter("loaithucan");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhomMA_Model kvm = new NhomMA_Model();
                Vector v = new Vector();
                v = kvm.SortSearchTTNMA(langid, order, loaithucan, tennhommonan, status);
                request.setAttribute("DSNMA", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("tennhommonan", tennhommonan);
                request.setAttribute("loaithucan", loaithucan);
                request.setAttribute("status", status);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchNMA.jsp");
                rd.forward(request, response);
            } else if (action.equals("CheckTenNMA")) {
                String nhomma = request.getParameter("nhomma");
                if (NhomMA_Model.CheckTenNMA(nhomma, langid)) {
                    out.print("exist");
                } else {
                    out.print("error");
                }
            } else if (action.equals("CheckTenNMAEdit")) {
                String nhomma = request.getParameter("nhomma");
                String manma = request.getParameter("manma");
                if (NhomMA_Model.CheckTenNMAEdit(manma, nhomma, langid)) {
                    out.print("exist");
                } else {
                    out.print("error");
                }
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
